<template>
       <!-- 点赞文章 -->
      <van-icon
        :color="attitude === 1 ? '#ff0000' : '#777'"
        :name="attitude === 1 ? 'good-job' : 'good-job-o'"
        @click="changeLike"
      />
</template>

<script>

import { addLikeAPI, deleteLikeAPI } from '../api/index.js'

export default {
  name: 'CllectArticle',
  // 用默认名称

  props: {
    attitude: {
      type: Number,
      default: -1
    },
    // value: {
    //   type: Boolean,
    //   default: true
    // },
    autId: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      btnLoading: false
    }
  },

  methods: {
    async changeLike () { // 改变关注状态
    // 触发改变时，开启加载状态
      if (this.btnLoading) return
      this.btnLoading = true
      try {
        if (this.attitude === 1) {
          // 当前已关注，需要取消关注
          await deleteLikeAPI(this.autId)
        } else {
          // 当前没有关注，需要添加关注
          await addLikeAPI(this.autId)
        }
        // 让父组件去完成关注状态的变化
        // this.$emit('update-followed', !this.isFollowed)
        // this.$emit('input',!this.value)
        this.$emit('update:attitude', !this.attitude === 1 ? -1 : 1)
        this.$toast(this.attitude !== 1 ? '文章点赞成功' : '取消文章点赞')
      } catch (error) {
        if (error.response && error.response.status === 400) {
          this.$toast('你不能关注自己')
        } else {
          this.$toast('操作失败')
        }
        console.log(error)
      }
      // 整个逻辑完成后关闭加载状态
      this.btnLoading = false
    }
  }
}
</script>

<style lang="less" scoped>

</style>
